package com.webchat.client.mall.service;


import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.exception.BusinessException;
import com.webchat.domain.vo.request.mall.CreateProductPaymentOrderReqVO;
import com.webchat.domain.vo.request.mall.PreCreateProductOrderReqVO;
import com.webchat.domain.vo.response.mall.MallUserProductOrderResponseVO;
import com.webchat.rmi.ugc.mall.MallProductOrderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

@Service
public class MallProductOrderService {

    @Autowired
    private MallProductOrderClient mallProductOrderClient;

    /**
     * 预创建订单，由底层订单服务（webchat-ugc）实现核心功能
     *
     * @param preCreateProductOrderReq
     * @return
     */
    public String preCreate(@RequestBody PreCreateProductOrderReqVO preCreateProductOrderReq) {

        APIResponseBean<String> response = mallProductOrderClient.preCreateOrder(preCreateProductOrderReq);
        if (APIResponseBeanUtil.isOk(response)) {
            return response.getData();
        }
        throw new BusinessException(response.getMsg());
    }

    /**
     * 获取用户商品订单数据
     *
     * @param userId
     * @param orderNo
     * @return
     */
    public MallUserProductOrderResponseVO getUserOrder(String userId, String orderNo) {

        APIResponseBean<MallUserProductOrderResponseVO> response = mallProductOrderClient.userOrder(userId, orderNo);
        if (APIResponseBeanUtil.isOk(response)) {
            return response.getData();
        }
        throw new BusinessException(response.getMsg());
    }

    /**
     * 商品订单创建交易订单
     *
     * @param createProductPaymentOrderReq
     * @return
     */
    public String createPaymentOrder(CreateProductPaymentOrderReqVO createProductPaymentOrderReq) {

        APIResponseBean<String> apiResponseBean = mallProductOrderClient.createPaymentOrder(createProductPaymentOrderReq);
        if (APIResponseBeanUtil.isOk(apiResponseBean)) {
            return apiResponseBean.getData();
        }
        throw new BusinessException(apiResponseBean.getMsg());
    }
}
